<template>
  <div class="info-container">
    <el-card style="height: 100%">
      <search-tool @onSearch="handleSearch"></search-tool>
      <div class="addmore">
        <el-button type="primary" @click="addMore" style="margin-bottom: 24px"
          >添加</el-button
        >
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
        v-loading="loading"
        height="75%"
        align-items="center"
      >
        <el-table-column label="序号" type="index" />
        <el-table-column prop="planRegulateForm.name" label="预案名称" />
        <el-table-column prop="planRegulateForm.type" label="预案类型" />
        <el-table-column
          prop="planRegulateForm.uploadingTime"
          label="上传时间"
        />
        <el-table-column
          prop="planRegulateForm.company"
          label="所属/编制单位"
        />
        <el-table-column label="操作">
          <template #default="scope">
            <div class="action-row">
              <!-- <el-icon
                @click.prevent="editRow(scope.$index, scope.row)"
                color="#17b6f9"
                :size="18"
                ><Document
              /></el-icon> -->
              <svg-icon
                icon="edit"
                @click.prevent="editRow(scope.$index, scope.row)"
              />
              <div class="has-more"></div>
              <!-- <el-icon
                @click="deleRow(scope.$index, scope.row)"
                color="#17b6f9"
                :size="18"
                ><Delete
              /></el-icon> -->
              <svg-icon
                icon="dele"
                @click.prevent="deleRow(scope.$index, scope.row)"
              />
              <div class="has-more"></div>
              <!-- <el-icon
                @click="down(scope.$index, scope.row)"
                color="#29d05b"
                size="18"
                ><Download
              /></el-icon> -->
              <svg-icon icon="down" @click="down(scope.$index, scope.row)" />
            </div>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        class="pagination"
        :currentPage="page"
        :page-sizes="[5, 10, 20]"
        :page-size="size"
        layout="prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <add-dialog
      :title="dialogTitle"
      :rowData="curRowData"
      :visible="visibleDialog"
      @onReload="reloadPage"
      @onCloes="onCloes"
    ></add-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import SearchTool from './components/SearchTool/index.vue'
import AddDialog from './components/AddDialog/index'
import { allList, ListDelet, ListId } from '@/api/reserveData'
import { ElMessageBox } from 'element-plus'
import { message } from '@/utils/message'
import { Base64 } from 'js-base64'
import { Delete, Download, Document } from '@element-plus/icons-vue'

const params = ref({})
const tableData = ref([])
const loading = ref(false)
const page = ref(1)
const total = ref(0)
const size = ref(8)
const dialogTitle = ref('新建工单')
const visibleDialog = ref(false)
const curRowData = ref({})

// 查询所有数据
const getListData = async (params) => {
  loading.value = true
  const res = await allList(params, page.value, size.value)
  console.log('res222', res)
  loading.value = false
  tableData.value = res.rows
  total.value = res.total
}
getListData()
//查询条件相关
const handleSearch = async (paramsData) => {
  loading.value = true
  var planRegulateForm = {
    endTime: paramsData.time[1],
    fileInfo: {
      fileId: '',
      fileName: '',
      fileSize: 0,
      fileUrl: '',
      type: ''
    },
    planRegulateForm: {
      company: '',
      id: '',
      isDeleted: '',
      name: paramsData.name,
      type: paramsData.type,
      ploadingTime: '',
      uploadingTime: ''
    },
    startTime: paramsData.time[0]
  }
  const res = await allList(planRegulateForm, page.value, size.value)
  console.log('res222', res)
  loading.value = false
  tableData.value = res.rows
}
const addMore = () => {
  dialogTitle.value = '新建预案'
  curRowData.value = {}
  visibleDialog.value = true
}
const teamDele = (row, indexa) => {}
const reloadPage = () => {
  getListData()
  visibleDialog.value = false
}
// 编辑信息
const editRow = (index, scoe) => {
  window.open(
    'http://36.7.170.39:8012/onlinePreview?url=' +
      Base64.encode(scoe.fileInfo.fileUrl)
  )
}
// 删除信息
const deleRow = (index, scop) => {
  console.log('scop', scop.planRegulateForm.id)
  ElMessageBox.confirm('确定删除此预案吗?', '删除预案', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  })
    .then(() => {
      // 删除
      deleList(scop.planRegulateForm.id)
    })
    .catch(() => {
      // catch error
    })
}
const down = async (index, options) => {
  loading.value = true
  let res = await ListId(options.planRegulateForm.id)
  // console.log('res223232', res)
  // console.log('下载', options.planRegulateForm.id)
  const url = res.fileInfo.fileUrl
  const filename = res.fileInfo.fileName
  const download = new XMLHttpRequest()
  download.open('GET', url, true)
  download.responseType = 'blob'
  download.onload = (e) => {
    const url = window.URL.createObjectURL(download.response)
    const a = document.createElement('a')
    a.href = url
    a.download = filename
    a.click()
  }
  download.send()
  loading.value = false
}
const deleList = async (id) => {
  console.log('id', id)
  const res = await ListDelet(id)
  message({ type: 'success', message: '删除成功!' })
  getListData()
}
const onCloes = () => {
  visibleDialog.value = false
  curRowData.value = {}
}
const handleSizeChange = (val) => {
  size.value = val
  getListData()
}
const handleCurrentChange = (val) => {
  page.value = val
  getListData()
}
</script>

<style lang="scss" scoped>
.info-container {
  width: 100%;
  height: 100%;
  .add-btn {
    width: 80px;
    height: 36px;
    font-size: 16px;
    font-family: Microsoft YaHei;
    color: #ff9502;
    box-sizing: border-box;
    cursor: pointer;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.39);
    border: 1px solid #ff9502;
    box-shadow: 0px 0px 4px rgba(206, 206, 206, 0.3);
    text-align: center;
    line-height: 36px;
    &:hover {
      background: #ff950236;
    }
  }
  .pagination {
    width: 100%;
    height: 32px;
    margin-top: 20px;
    display: flex;
    align-items: center;
    justify-content: end;
  }
  .action-row {
    display: flex;
    .action-btn {
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      line-height: 19px;
      cursor: pointer;
    }
    .has-more {
      width: 2px;
      height: 10px;
      margin-right: 10px;
      margin-left: 10px;
      margin-top: 5px;
    }
  }
  .color1 {
    color: #409eff;
  }
  .color2 {
    color: #67c23a;
  }
  .color3 {
    color: #f56c6c;
  }
}
</style>
